
import os
import pymysql
from common.my_path import CONF_PATH
from common.my_conf import My_Conf
from common.my_log import my_log


class Base_DB(object):

    def __init__(self):
        conf = My_Conf(os.path.join(CONF_PATH,'conf.ini'))
        self.con = pymysql.connect(host = conf.get_str('mysql','host'),
                              port = conf.get_int('mysql','port'),
                              user = conf.get_str('mysql','user'),
                              password = conf.get_str('mysql','password'),
                              database = conf.get_str('mysql','database')
                              )
        my_log.info('connect database......')

        #创建游标
        self.cur = self.con.cursor()

    #查询一条数据
    def get_one(self,sql):
        self.con.commit()
        my_log.info('implement sql:{}'.format(sql))
        self.cur.execute(sql)
        res = self.cur.fetchone()
        return res

    #查询多条数据
    def get_all(self,sql):
        self.con.commit()
        my_log.info('implement sql:{}'.format(sql))
        self.cur.execute(sql)
        res = self.cur.fetchall()
        return res

    #查询是否有数据
    def get_count(self,sql):
        self.con.commit()
        my_log.info('implement sql:{}'.format(sql))
        count = self.cur.execute(sql)
        return count

    #关闭数据库
    def close(self):
        self.cur.close()
        self.con.close()



if __name__ == '__main__':
    # from common.my_path import DATA_PATH
    # from common.read_excel import Read_Excel
    # excle = Read_Excel(DATA_PATH,'recharge')
    # data = excle.read_data()
    # print(data[1]['data'])
    # print(type(eval(data[1]['data'])))

    DB = Base_DB()
    # sql = "select * from member where mobile_phone = '18860639042'"
    aa = DB.get_one("select * from member where mobile_phone = '18860639042'")[5]

    print(aa)